import java.util.HashMap;
import java.util.Map;

public class LongestPalindrome {

    /**
     * https://leetcode-cn.com/problems/longest-palindrome/
     * https://www.lintcode.com/problem/longest-palindrome/description
     * @param s: a string which consists of lowercase or uppercase letters
     * @return: the length of the longest palindromes that can be built
     */
    public int longestPalindrome(String s) {
        //也可以用集合，先塞进去，然后，发现有了，再删掉。 也会方便点。
        if(s==null || s.length() == 0) return 0;

        char[] ss = s.toCharArray();
        int n = ss.length;
        Map<String, Integer> counts= new HashMap<String, Integer>();

        int longest = 0;
        for(int i=0; i<n;i++){
            Integer c = counts.get(String.valueOf(ss[i]));
            if(c == null){
                c = 0;
            }
            counts.put(String.valueOf(ss[i]), c + 1);
        }

        boolean hasSingle = false;
        for(Map.Entry<String, Integer> entry : counts.entrySet()){
            if(entry.getValue() >= 2){
                if(entry.getValue() % 2 == 0){
                    longest += entry.getValue();
                }else {
                    if(!hasSingle){
                        longest += entry.getValue();
                        hasSingle = true;
                    }else{
                        longest += entry.getValue() - 1;
                    }

                }
            }else if(entry.getValue() == 1 && !hasSingle){
                longest++;
                hasSingle = true;
            }
        }

        return longest;
    }

    public static void main(String[] args){
        LongestPalindrome l = new LongestPalindrome();
        l.longestPalindrome("abccccdd");

    }
}